package  
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.printing.PrintJob;
	import flash.printing.PrintJobOrientation;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import gui.buttons.BaseButton;
	import gui.buttons.LabelButton;
	import gui.buttons.RadioButton;
	import gui.PositionInfo;
	import gui.ScrollBar;
	import utils.MathUtil;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class TestPage extends Sprite
	{
		private var btn:LabelButton;
		private var radBtn:RadioButton;
		private var a:Point;
		private var b:Point;
		private var c:Point;
		private var d:Point;
		private var sizeA:Number = 50;
		private var sizeB:Number = 100;
		private var currentPoint:Point;
		private var movX:Number = 0;
		private var movY:Number = 0;
		
		 private var sheet1:Sprite;
        private var sheet2:Sprite;
		
		public function TestPage() 
		{
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
		}
		
		private function handleAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
			//initPage();
			//initScrollTest();
			//initTestIntersect();
			//initTestPrint();
		}
		
		private function initTestPrint():void 
		{
			  initPrint();
//            printOnePerPage();
           printTwoPerPage();
 //          printTopHalf();
            //draw();
		}
		
		private function draw():void {
            var sheetWidth:Number = this.stage.stageWidth/2;
            var sheetHeight:Number = this.stage.stageHeight;
            
            addChild(sheet1);
            sheet1.width = sheetWidth;
            sheet1.height = sheetHeight;
            
            addChild(sheet2);
            sheet2.width = sheetWidth;
            sheet2.height = sheetHeight;
            sheet2.x = sheet1.width;
        }        
		private function initPrint():void 
		{
			 sheet1 = new Sprite();
            createSheet(sheet1, "Once upon a time...", {x:10, y:50, width:80, height:130});

            sheet2 = new Sprite();
            createSheet(sheet2, "There was a great story to tell, and it ended quickly.\n\nThe end.", null);
		}
		
		  private function createSheet(sheet:Sprite, str:String, imgValue:Object):void 
		  {
            sheet.graphics.beginFill(0xEEEEEE);
            sheet.graphics.lineStyle(1, 0x000000);
            sheet.graphics.drawRect(0, 0, 100, 200);
            sheet.graphics.endFill();
            
            var txt:TextField = new TextField();
            txt.height = 200;
            txt.width = 100;
            txt.wordWrap = true;
            txt.text = str;
            
            if(imgValue != null) {
                var img:Sprite = new Sprite();
                img.graphics.beginFill(0xFFFFFF);
                img.graphics.drawRect(imgValue.x, imgValue.y, imgValue.width, imgValue.height);
                img.graphics.endFill();
                sheet.addChild(img);
            }
            sheet.addChild(txt);
        }
		
		 private function printTwoPerPage():void {
            var pj:PrintJob = new PrintJob();
            var pagesToPrint:uint = 0;
            if(pj.start()) {                
                if(pj.orientation == PrintJobOrientation.PORTRAIT) {
                    throw new Error("Without embedding fonts you must print two sheets per page with an orientation of landscape.");
                }
                
                sheet1.height = pj.pageHeight;
                sheet1.width = pj.pageWidth/2;
                sheet2.height = pj.pageHeight;
                sheet2.width = pj.pageWidth/2;

                var sheets:Sprite = new Sprite();
                sheets.addChild(sheet1);
                sheets.addChild(sheet2);
                sheets.getChildAt(1).x = sheets.getChildAt(0).width;
                try {
                    pj.addPage(sheets);
                    pagesToPrint++;
                }
                catch(e:Error) {
                    // do nothing
                }

                if(pagesToPrint > 0) {
                    pj.send();
                }
            }
        }
		
		 private function printOnePerPage():void 
		 {
            var pj:PrintJob = new PrintJob();
            var pagesToPrint:uint = 0;
            if (pj.start()) 
			{                
                if (pj.orientation == PrintJobOrientation.LANDSCAPE) 
				{    
                    throw new Error("Without embedding fonts\
					you must print one sheet per page with an orientation of portrait.");
                }
                
                sheet1.height = pj.pageHeight;
                sheet1.width = pj.pageWidth;
                sheet2.height = pj.pageHeight;
                sheet2.width = pj.pageWidth;

                try {
                    pj.addPage(sheet1);
                    pagesToPrint++;
                }
                catch(e:Error) {
                    // do nothing
                }

                try {
                    pj.addPage(sheet2);
                    pagesToPrint++;
                }
                catch(e:Error) {
                    // do nothing
                }

                if (pagesToPrint > 0) 
				{
                    pj.send();
                }
            }
        }
        
        
		private function initTestIntersect():void 
		{
			a = new Point(50, 50);
			b = new Point(a.x, a.y + sizeA);
			
			c = new Point(80 , 100);
			d = new Point(c.x, c.y + sizeB);
			
			currentPoint = a;
			
			addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
			
		}
		
		private function handleKeyUp(e:KeyboardEvent):void 
		{
			movY = 0;
			movX = 0;
		}
		
		private function handleKeyDown(e:KeyboardEvent):void 
		{
			var speed:Number = 1;
			switch(e.keyCode)
			{
				case Keyboard.UP:
					movY = -speed;
					movX = 0;
				break;
				case Keyboard.DOWN:
						movY = speed;
						movX = 0;
					break;
				case Keyboard.RIGHT:
						movY = 0;
						movX = speed;
					break;
				case Keyboard.LEFT:
						movY = 0;
						movX = -speed;
					break;
				case Keyboard.SPACE:
						if (currentPoint == a)
						{
							currentPoint = c;
						}else
						{
							currentPoint = a;
						}
					break;
			}
		}
		
		private function handleEnterFrame(e:Event):void 
		{
			if (currentPoint != null)
			{
				if (currentPoint == a)
				{
					a.x += movX;
					a.y += movY;
					
					b.x = a.x, 
					b.y = a.y + sizeA;
				}
				
				if (currentPoint == c)
				{
					c.x += movX;
					c.y += movY;
					
					d.x = c.x + sizeB; 
					d.y = c.y + sizeB;
				}
			}
			var gr:Graphics = graphics;
			gr.clear();
			
			var colorA:uint =  0x00FF00;
			var colorB:uint =  0x000FF;
			// check intersect
			var intersect:Boolean = MathUtil.intersectSegments(a, b, c, d);
			if (intersect)
			{
				colorA = 0xFF0000;
				colorB = 0xFF0000;
			}
			//segment A
			gr.lineStyle(1.5, colorA);
			gr.moveTo(a.x, a.y);
			gr.lineTo(b.x, b.y);
			
			//segment B
			gr.lineStyle(1.5,colorB);
			gr.moveTo(c.x, c.y);
			gr.lineTo(d.x, d.y);
		}
		
		private function initScrollTest():void 
		{
			var scroll:ScrollBar = new ScrollBar();
			addChild(scroll);
		}
		
		private function initPage():void 
		{
			btn = new LabelButton(this, 100, 100);
			//radBtn = new RadioButton(this, 100, 50);
			//radBtn.label = "Ololo";
			//btn.setStyle(BaseButton.UP_SKIN, new UpSkin());
			/*btn.setStyle(BaseButton.OVER_SKIN, new OvesrSkin());
			btn.setStyle(BaseButton.DOWN_SKIN, new DownSkin());
			
			btn.setStyle(BaseButton.SELECTED_UP_SKIN, new SelectedUpSkin());
			btn.setStyle(BaseButton.SELECTED_OVER_SKIN, new SelectedOvesrSkin());
			btn.setStyle(BaseButton.SELECTED_DOWN_SKIN, new SelectedDownSkin());*/
			
			btn.button.addEventListener(MouseEvent.CLICK, handleMouseClick);
			btn.addEventListener(Event.CHANGE, handleChange);
			btn.toggle = true;
			
			btn.label = "Ololo";
			btn.textPosition.align = PositionInfo.ALIGN_CENTR_X;
			btn.textPosition.addAlign(PositionInfo.ALIGN_CENTR_Y);
			btn.textPosition.posY = 15;
			//btn.textPosition.centeredByY = false;
			//btn.textPosition = new PositionInfo(4, 4);
			//btn.hitArea = new RadBtnHitArea();
			for (var i:int = 0; i < 5; i++ )
			{
				var b:RadioButton = new RadioButton(this, 50, 50 + 30* i);
				b.label = "Button " + i.toString();
				b.groupName = "11";
				//b.addEventListener(MouseEvent.CLICK, radButtonClick);
				
				if (i == 0)
				{
					b.selected = true;
				}
				
				b = new RadioButton(this, 250, 50 + 50* i);
				b.label = "Button " + i.toString();
				b.height = 40;
				b.textPosition.align = PositionInfo.ALIGN_LEFT;
				b.textPosition.addAlign(PositionInfo.ALIGN_TOP);
				b.textPosition.leftPadding = 1;
				b.textPosition.topPadding = 1;
				b.textPosition.posY = 5;
				b.textPosition.posX = 5;
				
				b.iconPosition.align = PositionInfo.ALIGN_RIGHT;
				b.iconPosition.addAlign(PositionInfo.ALIGN_BOTTOM);
				b.iconPosition.rightPadding = 5;
				b.iconPosition.bottomPadding = 1;
				b.groupName = "22";
				b.setStyle(BaseButton.SELECTED_UP_SKIN, new SelectedDownSkin());
				b.setStyle(BaseButton.SELECTED_OVER_SKIN, new SelectedDownSkin());
				b.setStyle(BaseButton.SELECTED_DOWN_SKIN, new SelectedDownSkin());
				
				b.setStyle(BaseButton.UP_SKIN, new UpSkin());
				b.setStyle(BaseButton.OVER_SKIN, new UpSkin());
				b.setStyle(BaseButton.DOWN_SKIN, new UpSkin());
				
				
				b.setStyle(RadioButton.UP_ICON, new RadioButtonIcon());
				b.setStyle(RadioButton.OVER_ICON, new RadioButtonIcon());
				b.setStyle(RadioButton.DOWN_ICON, new RadioButtonIcon());
				
				b.setStyle(RadioButton.SELECTED_UP_ICON, new RadioButtonSelectedIcon());
				b.setStyle(RadioButton.SELECTED_OVER_ICON, new RadioButtonSelectedIcon());
				b.setStyle(RadioButton.SELECTED_DOWN_ICON, new RadioButtonSelectedIcon());
				//b.addEventListener(MouseEvent.CLICK, radButtonClick);
				
				if (i == 0)
				{
					b.selected = true;
				}
			}
		}
		
		
		private function drawCircle(gr:Graphics, color:uint, radius:Number):void
		{
			gr.lineStyle(1, 0x000000);
			gr.beginFill(color, 1);
			gr.drawCircle( 0, 0, radius);
			gr.endFill();
		}
		
		private function handleChange(e:Event):void 
		{
			trace("Change");
		}
		
		private function handleMouseClick(e:MouseEvent):void 
		{
			trace("click");
			btn.selected = !btn.selected;
			btn.width += 10;
			
		}
	}

}